//
//  GroupNotiViewController.m
//  Findif
//
//  Created by Mac on 2019/3/11.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "GroupNotiViewController.h"
#import "NotiListTableViewCell.h"
#import "MessageNotiModel.h"
#import "FroupNotiDetailViewController.h"

@interface GroupNotiViewController ()<UITableViewDelegate,UITableViewDataSource,NotiListTableViewCellCellDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <MessageNotiModel *> * modelArray;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;

@end

@implementation GroupNotiViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setupNav];
    [self setupUI];
}


- (void)setModelArray:(NSArray<MessageNotiModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    [self.tableView reloadData];
}

- (void)loadData{
    
//    self.modelArray = nil;
    NSString * user_id = DEF_GET_OBJECT(@"id");
    [[ZRNetworkTool sharedTool] getGroupInformWithUser_id:user_id WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            self.modelArray = [NSArray yy_modelArrayWithClass:[MessageNotiModel class] json:dict[@"dataArr"]];
        }
        
    }];
    
    [self.tableView.mj_header endRefreshing];
}

- (void)setupNav{
    
    self.title = @"社团通知";
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 15 * SCREENH6)];
    
    [rightButton setTitle:@"清空" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didDeteleButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF0F0F0);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF0F0F0);
    tableView.rowHeight = 70 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[NotiListTableViewCell class] forCellReuseIdentifier:@"NotiListTableViewCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadData];
    }];
    
    [self.view addSubview:self.tableView];
    
    
    UIImageView * spaceImageView = [UIImageView new];

    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;

    self.spaceImageView = spaceImageView;

    [self.tableView addSubview:self.spaceImageView];


    UILabel * spaceLabel = [UILabel new];

    spaceLabel.text = @"暂无任何社团通知";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;

    self.spaceLabel = spaceLabel;

    [self.tableView addSubview:self.spaceLabel];
}

- (void)viewDidLayoutSubviews{
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.tableView);
        make.width.mas_equalTo(169 * SCREENH6);
        make.height.mas_equalTo(136 * SCREENH6);
    }];

    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.modelArray.count > 0) {

        self.spaceImageView.hidden = YES;
        self.spaceLabel.hidden = YES;
    }else{

        self.spaceImageView.hidden = NO;
        self.spaceLabel.hidden = NO;
    }
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NotiListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"NotiListTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.row];
    cell.delegate = self;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageNotiModel * model = self.modelArray[indexPath.row];
    
    HiddenBar;
    
    FroupNotiDetailViewController * detailVC = [FroupNotiDetailViewController new];
    
    detailVC.ID = model.id;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

//    if (indexPath.row == 0) {
//
       return YES;
//    }
//    return NO;
}

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        MessageNotiModel * model = self.modelArray[indexPath.row];
        NSString * ID = [NSString stringWithFormat:@"%@",model.id];
        NSDictionary * dict = @{@"delete_type":@"one",@"id":ID};
        
        [[ZRNetworkTool sharedTool] deleteGroupInformWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                [self loadData];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

    return @"删除";
}


//- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)){
//    //删除
//    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//
//        MessageNotiModel * model = self.modelArray[indexPath.row];
//        NSString * ID = [NSString stringWithFormat:@"%@",model.id];
//        NSDictionary * dict = @{@"delete_type":@"one",@"id":ID};
//
//        [[ZRNetworkTool sharedTool] deleteGroupInformWithDict:dict WithBlock:^(BOOL success, id responseData) {
//
//            if (success) {
//
//                [self loadData];
//            }else{
//
//                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                               message:responseData[@"message"]
//                                                                        preferredStyle:UIAlertControllerStyleAlert];
//
//                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                                      handler:^(UIAlertAction * action) {
//
//                                                                      }];
//
//                [alert addAction:defaultAction];
//                [self presentViewController:alert animated:YES completion:nil];
//            }
//        }];
//
//
//        completionHandler (YES);
//    }];
//    deleteRowAction.image = [UIImage imageNamed:@"删除"];
//    deleteRowAction.backgroundColor = [UIColor redColor];
//
//    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
//    return config;
//}


- (void)didDeteleButton{
    
    NSDictionary * dict = @{@"delete_type":@"all"};
    
    [[ZRNetworkTool sharedTool] deleteGroupInformWithDict:dict WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.modelArray = nil;
            [self loadData];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}


- (void)clickAgreeButtonWithID:(NSString *)ID{

    ShowHUD;

    [[ZRNetworkTool sharedTool] updateGroupInformWithID:ID WithAudit_status:@"1" WithBlock:^(BOOL success, id responseData) {

        HiddenHUD;

        if (success) {

            [self loadData];
        }else{

            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {

                                                                  }];

            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}


#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}

- (UIColor *)xy_noDataViewMessageColor {
    
    return [UIColor blackColor];
}

@end
